from sqlalchemy import Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from init.mysql import engine

Base = declarative_base()


class StockTakeHistory(Base):
    __tablename__ = 'stock_take_history'
    id = Column(Integer, primary_key=True, autoincrement=True)
    dm = Column(String(6))
    rq = Column(String(10))
    rzrqye = Column(String(20))
    rzrqyecz = Column(String(20))
    p = Column(String(20))
    zdf1 = Column(String(20))
    zdf3 = Column(String(20))
    zdf5 = Column(String(20))
    zdf10 = Column(String(20))
    rzye = Column(String(20))
    rzyezb = Column(String(20))
    rzmre1 = Column(String(20))
    rzmre3 = Column(String(20))
    rzmre5 = Column(String(20))
    rzmre10 = Column(String(20))
    rzche1 = Column(String(20))
    rzche3 = Column(String(20))
    rzche5 = Column(String(20))
    rzche10 = Column(String(20))
    rzjm1 = Column(String(20))
    rzjm3 = Column(String(20))
    rzjm5 = Column(String(20))
    rzjm10 = Column(String(20))
    rqye = Column(String(20))
    rqyl = Column(String(20))
    rqmcl1 = Column(String(20))
    rqmcl3 = Column(String(20))
    rqmcl5 = Column(String(20))
    rqmcl10 = Column(String(20))
    rqchl1 = Column(String(20))
    rqchl3 = Column(String(20))
    rqchl5 = Column(String(20))
    rqchl10 = Column(String(20))
    rqjmc1 = Column(String(20))
    rqjmc3 = Column(String(20))
    rqjmc5 = Column(String(20))
    rqjmc10 = Column(String(20))

    def __init__(self, dm, rq, rzrqye, rzrqyecz, p, zdf1, zdf3, zdf5, zdf10, rzye, rzyezb, rzmre1, rzmre3, rzmre5,
                 rzmre10, rzche1, rzche3, rzche5, rzche10, rzjm1, rzjm3, rzjm5, rzjm10, rqye, rqyl, rqmcl1, rqmcl3,
                 rqmcl5, rqmcl10, rqchl1, rqchl3, rqchl5, rqchl10, rqjmc1, rqjmc3, rqjmc5, rqjmc10):
        self.dm = dm
        self.rq = rq
        self.rzrqye = rzrqye
        self.rzrqyecz = rzrqyecz
        self.p = p
        self.zdf1 = zdf1
        self.zdf3 = zdf3
        self.zdf5 = zdf5
        self.zdf10 = zdf10
        self.rzye = rzye
        self.rzyezb = rzyezb
        self.rzmre1 = rzmre1
        self.rzmre3 = rzmre3
        self.rzmre5 = rzmre5
        self.rzmre10 = rzmre10
        self.rzche1 = rzche1
        self.rzche3 = rzche3
        self.rzche5 = rzche5
        self.rzche10 = rzche10
        self.rzjm1 = rzjm1
        self.rzjm3 = rzjm3
        self.rzjm5 = rzjm5
        self.rzjm10 = rzjm10
        self.rqye = rqye
        self.rqyl = rqyl
        self.rqmcl1 = rqmcl1
        self.rqmcl3 = rqmcl3
        self.rqmcl5 = rqmcl5
        self.rqmcl10 = rqmcl10
        self.rqchl1 = rqchl1
        self.rqchl3 = rqchl3
        self.rqchl5 = rqchl5
        self.rqchl10 = rqchl10
        self.rqjmc1 = rqjmc1
        self.rqjmc3 = rqjmc3
        self.rqjmc5 = rqjmc5
        self.rqjmc10 = rqjmc10

    @staticmethod
    def not_exist_insert(code, data_list):
        session = sessionmaker(bind=engine)()
        for data in data_list:
            existing_stock_take_history = session.query(StockTakeHistory).filter_by(dm=code, rq=data['rq']).first()
            if existing_stock_take_history:
                continue
            else:
                session.add(StockTakeHistory(dm=code, rq=data['rq'], rzrqye=data['rzrqye'], rzrqyecz=data['rzrqyecz'],
                                             p=data['p'], zdf1=data['zdf1'], zdf3=data['zdf3'], zdf5=data['zdf5'],
                                             zdf10=data['zdf10'], rzye=data['rzye'], rzyezb=data['rzyezb'],
                                             rzmre1=data['rzmre1'], rzmre3=data['rzmre3'], rzmre5=data['rzmre5'],
                                             rzmre10=data['rzmre10'], rzche1=data['rzche1'], rzche3=data['rzche3'],
                                             rzche5=data['rzche5'], rzche10=data['rzche10'], rzjm1=data['rzjm1'],
                                             rzjm3=data['rzjm3'], rzjm5=data['rzjm5'], rzjm10=data['rzjm10'],
                                             rqye=data['rqye'], rqyl=data['rqyl'], rqmcl1=data['rqmcl1'],
                                             rqmcl3=data['rqmcl3'], rqmcl5=data['rqmcl5'], rqmcl10=data['rqmcl10'],
                                             rqchl1=data['rqchl1'], rqchl3=data['rqchl3'], rqchl5=data['rqchl5'],
                                             rqchl10=data['rqchl10'], rqjmc1=data['rqjmc1'], rqjmc3=data['rqjmc3'],
                                             rqjmc5=data['rqjmc5'], rqjmc10=data['rqjmc10']))
        session.commit()


Base.metadata.create_all(engine)
